package com.winspread.ui.squareview.base

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.annotation.Nullable
import com.winspread.ui.R

/**
 *Author: Amin
 *Data: 2022/4/11 10:24
 *
 *Description: 主布局样式
 *
 * 如：正常 背景 弧度，背景
 *
 */
class SquareNormalView(
    @Nullable context: Context?,
    @Nullable attrs: AttributeSet?,
) {

    val defValue = 0F

    //转角 弧度
    private var corner = 0F

    //转角位置 弧度
    var cornerLt: Float = 0F
    var cornerRt: Float = 0F
    var cornerLb: Float = 0F
    var cornerRb: Float = 0F


    var dashWidth: Float = 0F
    var dashGap: Float = 0F
    var lineColor: Int = 0
    var fillColor: Int = 0
    var lineWidth: Float = 0F

    // 文字颜色
    var textColor: Int = 0

    // 图片
    var topDraw: Drawable? = null
    var leftDraw: Drawable? = null
    var bottomDraw: Drawable? = null
    var rightDraw: Drawable? = null


    init {
        val obtainStyledAttributes = context?.obtainStyledAttributes(attrs, R.styleable.View)
        obtainStyledAttributes?.let {
            //圆角
            corner = it.getDimension(R.styleable.View_corner, defValue)
            cornerLt = it.getDimension(R.styleable.View_corner_left_top, defValue)
            cornerLb = it.getDimension(R.styleable.View_corner_left_bottom, defValue)
            cornerRt = it.getDimension(R.styleable.View_corner_right_top, defValue)
            cornerRb = it.getDimension(R.styleable.View_corner_right_bottom, defValue)
            //线宽
            lineWidth = it.getDimension(R.styleable.View_line_width, defValue)
            //虚线宽度
            dashWidth = it.getDimension(R.styleable.View_dash_width, defValue)
            // 虚线间距
            dashGap = it.getDimension(R.styleable.View_dash_gap, 2F)
            // 线颜色
            lineColor = it.getColor(R.styleable.View_line_color, Color.TRANSPARENT)
            // 填充颜色
            fillColor = it.getColor(R.styleable.View_background_color, Color.TRANSPARENT)
            // 字体颜色
            textColor = it.getColor(R.styleable.View_text_color, 0)
            // draw 图片
            topDraw = it.getDrawable(R.styleable.View_drawable_top)
            leftDraw = it.getDrawable(R.styleable.View_drawable_left)
            rightDraw = it.getDrawable(R.styleable.View_drawable_right)
            bottomDraw = it.getDrawable(R.styleable.View_drawable_bottom)
        }
    }


    fun setCorner(value: Float) {
        corner = value
        cornerLt = 0F
        cornerLb = 0F
        cornerRt = 0F
        cornerRb = 0F
    }

    fun getCorner(): Float {
        return corner
    }

    fun getBackGround(): Drawable {
        val drawableBuilder = DrawableBuilder()
        if (defValue != corner) {
            drawableBuilder.corner(floatArrayOf(
                corner, corner, corner, corner,
                corner, corner, corner, corner
            ))
        }
        if (defValue != cornerLt || defValue != cornerLb || defValue != cornerRt || defValue != cornerRb) {
            drawableBuilder.corner(
                floatArrayOf(
                    cornerLt, cornerLt, cornerRt, cornerRt,
                    cornerRb, cornerRb, cornerLb, cornerLb
                )
            )
        }
        if (defValue != lineWidth) {
            drawableBuilder.line().lineWidth(lineWidth).lineColor(lineColor)
        }

        if (defValue != dashWidth) {
            drawableBuilder.dash().dashWidth(dashWidth).dashGap(dashGap)
        }

        if (fillColor != 0) {
            drawableBuilder.fill(fillColor)
        }

        return drawableBuilder.build()
    }


}